from selenium import webdriver


class SeleniumDemo(object):
    def __init__(self):
        self.url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
        self.url_add_emp = 'http://127.0.0.1:8000/admin/employee/employee/add/'
        self.browser = webdriver.Chrome()
        self.browser.maximize_window()

    def login(self):
        self.browser.get(self.url)
        username = self.browser.find_element_by_id('id_username')
        password = self.browser.find_element_by_id('id_password')
        login_btn = self.browser.find_element_by_xpath('//*[@id="login-form"]/div[3]/input')
        username.send_keys('admin')
        password.send_keys('admin')
        login_btn.click()

    def add_employee(self):
        add_link = self.browser.find_element_by_xpath('//*[@id="content-main"]/div[2]/table/tbody/tr/td[1]/a')
        add_link.click()
        if self.browser.current_url != self.url_add_emp:
            self.browser.get(self.url_add_emp)
        code = self.browser.find_element_by_name('code')
        name = self.browser.find_element_by_name('name')
        btn_save = self.browser.find_element_by_xpath('//*[@id="employee_form"]/div/div/input[1]')
        code.send_keys('E01')
        name.send_keys('Jacky')
        btn_save.click()


if __name__ == '__main__':
    sd = SeleniumDemo()
    sd.login()
    sd.add_employee()
